package cn.gjxblog.core.social.qq.config;

import cn.gjxblog.core.properties.QQProperties;
import cn.gjxblog.core.properties.SecurityProperties;
import cn.gjxblog.core.social.qq.connect.QQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.social.UserIdSource;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/1/18 9:01
 */
@Configuration
@ConditionalOnProperty(prefix = "gjxblog.security.social.qq",name="app-id")
public class QQConfig extends SocialConfigurerAdapter {


    @Autowired
    private SecurityProperties securityProperties;

    public QQConfig() {
        super();
    }

    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer connectionFactoryConfigurer, Environment environment) {
        QQProperties qq = securityProperties.getSocial().getQq();
        QQConnectionFactory qqConnectionFactory = new QQConnectionFactory(qq.getProviderId(), qq.getAppId(), qq.getAppSecret());
        connectionFactoryConfigurer.addConnectionFactory(qqConnectionFactory);
    }

    @Override
    public UserIdSource getUserIdSource() {
        return new UserIdSource() {
            public String getUserId() {
                return "anonymous";
            }
        };
    }

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        return  null;
    }
}
